package com.holystone.orca.base.controller;

import com.holystone.orca.sys.common.SystemConstant;
import com.holystone.orca.sys.model.StaffVO;
import com.holystone.orca.sys.mybatis.model.SysPrivilege;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class BaseController implements Controller {
	private String successMsg;//成功信息
	private String errorMsg;//错误信息
	private String warningMsg;//警告信息
	private Integer pageType;//页面类型
	/**
	 * HttpServletRequest 对象
	 */
	public HttpServletRequest request() {
		ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
		HttpServletRequest request=attr.getRequest();
		return request;
	}
	/**
	 * HttpSession 对象
	 */
	public HttpSession session() {
		ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpSession contextSess = attr.getRequest().getSession(true);
		return contextSess;
	}
	/**
	 * 设置成功提示信息
	 */
	public void setSuccessMsg(int successMsg) {
		this.successMsg  = String.valueOf(successMsg);
	}
	/**
	 * 设置自定义成功信息
	 */
	public void setSuccessMsg(String successMsg) {
		this.successMsg  = successMsg;
	}
	/**
	 * 设置错误提示信息
	 */
	public void setErrorMsg(int errorMsg) {
		this.errorMsg =String.valueOf(errorMsg);
	}
	/**
	 * 设置自定义错误提示信息
	 */
	public void setErrorMsg(String errorMsg) {
		this.errorMsg =errorMsg;
	}
	/**
	 * 设置警告提示信息
	 */
	public void setWarningMsg(int warningMsg) {
		this.warningMsg = String.valueOf(warningMsg);
	}
	/**
	 * 设置自定义警告提示信息
	 */
	public void setWarningMsg(String warningMsg) {
		this.warningMsg = warningMsg;
	}
	/**
	 * 获取当前用户的staffVO
	 */
	public StaffVO getCurrentStaffVO(){
		StaffVO vo=(StaffVO) session().getAttribute(SystemConstant.SESSION_STAFF_KEY);
		return vo;
	}
	/**
	 * 设置返回页面类型
	 */
	public void setPageType(Integer pageType) {
		this.pageType = pageType;
	}
	
	
	public Integer getPageType() {
		return pageType;
	}
	private String getErrorMsg() {
		return errorMsg;
	}
	
	private String getSuccessMsg() {
		return successMsg;
	}
	
	private String getWarningMsg() {
		return warningMsg;
	}
	
	/**
	 * 获得当前登录用户的ID
	 */
	public String getCurrentStaffId() {
		// StaffVO staffVO=(StaffVO) this.session().getAttribute(this.SESSION_STAFF_KEY);
		return getCurrentStaffVO().getStaffId().toString();
	}

	/**
	 * 校验当前登录用户权限是否有效
	 */
	public Boolean checkPersonPrivilege() {
		String url = request().getServletPath();
		boolean flag = false;
		StaffVO staffVO = getCurrentStaffVO();
		for(SysPrivilege privilege : staffVO.getPrivilegeList()){
			if(("/"+privilege.getProgCode()).equals(url)){
				flag = true;
				break;
			}
		}
		return flag;
	}
}
